import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_module/app/app.dart';
import 'package:flutter_module/base/base.dart';
import 'package:flutter_module/user/model_user.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// ViewModel + Model
class CounterBloc extends Cubit<UserBean> {
  CounterBloc() : super(UserBean("", "", 0, ""));

  void upCount() {
    state.age++;
    emit(UserBean.fromMap(state.toMap()));
  }

  void upUser(UserBean user) {
    emit(user);
  }
}

/// View 静态
class PageStatelessBloc extends BaseWidget {
  const PageStatelessBloc({super.key});

  @override
  Widget setContent(BuildContext context) {
    print("构建 - widget");
    return BlocProvider(
      create: (BuildContext context) => CounterBloc(),
      child: BlocBuilder<CounterBloc, UserBean>(
        builder: (context, UserBean user) {
          print("user: ${user.toString()}");
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "Bloc",
                style: TextStyle(color: Colors.black, fontSize: 36.w),
              ),
              Expanded(child: Container()),
              Text(
                context.watch<CounterBloc>().state.name,
                style: TextStyle(color: Colors.black, fontSize: 36.w),
              ),
              Text(
                "${context.watch<CounterBloc>().state.age}",
                style: TextStyle(color: Colors.black, fontSize: 36.w),
              ),
              GestureDetector(
                onTap: () {
                  context.read<CounterBloc>().upCount();
                },
                child: Text(
                  "Count++",
                  style: TextStyle(color: Colors.black, fontSize: 36.w),
                ),
              ),
              GestureDetector(
                onTap: () {
                  context.read<RoutePageState>().toLogin();
                },
                child: Text(
                  "登录",
                  style: TextStyle(color: Colors.black, fontSize: 36.w),
                ),
              ),
              Expanded(child: Container()),
            ],
          );
        },
      ),
    );
  }
}

/// View 动态
class PageBloc extends BaseStateWidget {
  const PageBloc({super.key});

  @override
  StatefulElement createElement() {
    print("构建 element");
    return super.createElement();
  }

  @override
  BaseState<BaseStateWidget> buildState() => PageBlocState();
}

class PageBlocState extends BaseState<PageBloc> {
  @override
  Widget setContent(BuildContext context) {
    print("构建 - widget");
    return BlocProvider(
      create: (_) => CounterBloc(),
      child: BlocBuilder<CounterBloc, UserBean>(
        builder: (context, userBean) {
          print("user: ${userBean.toString()}");
          return Column(
            children: [
              Expanded(child: Container()),
              Text(
                "${userBean.age}",
                style: TextStyle(color: Colors.black, fontSize: 36.w),
              ),
              GestureDetector(
                onTap: () {
                  context.read<CounterBloc>().upCount();
                },
                child: Text(
                  "Count++",
                  style: TextStyle(color: Colors.black, fontSize: 36.w),
                ),
              ),
              GestureDetector(
                onTap: () {
                  context.read<RoutePageState>().toLogin();
                },
                child: Text(
                  "登录",
                  style: TextStyle(color: Colors.black, fontSize: 36.w),
                ),
              ),
              Expanded(child: Container()),
            ],
          );
        },
      ),
    );
  }
}
